package com.sskj.mine;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.Response;
import com.lzy.okrx2.adapter.FlowableBody;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.MD5Util;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.SPConfig;
import com.sskj.lib.http.JsonCallBack;
import com.sskj.lib.http.JsonConverter;
import com.sskj.lib.util.CommonUtil;
import com.sskj.mine.asset.data.CoinTypeBean;
import com.sskj.mine.asset.data.RechargeTypeBean;
import com.sskj.mine.asset.data.TradeAccountBean;
import com.sskj.mine.bean.BuyBalanceBean;
import com.sskj.mine.bean.PayWayListBean;
import com.sskj.mine.bean.ServiceChargeBean;
import com.sskj.mine.http.HttpConfig;
import com.sskj.mine.presenter.BasePresenter;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Flowable;

public class ShellPresenter extends BasePresenter<ShellActivity> {

//    public void getBalance(String type) {
//        OkGo.<HttpData<BuyBalanceBean>>post(HttpConfig.BASE_URL + HttpConfig.BUY_BALANCE)
//                .params("code", type)
//                .execute(new JsonCallBack<HttpData<BuyBalanceBean>>(this) {
//                    @Override
//                    public void onSuccess(Response<HttpData<BuyBalanceBean>> response) {
//                        HttpData body = response.body();
//                        if (body.status == HttpConfig.OK) {
//                            mView.updateUi(response.body().getData());
//                        }
//                    }
//                });
//    }

    public void getAllCoin(boolean isCreate) {

        httpService.getAllCoinInfo(isCreate)
                .execute(new JsonCallBack<HttpData<List<CoinTypeBean>>>() {
                    @Override
                    public void onSuccess(Response<HttpData<List<CoinTypeBean>>> response) {
                        HttpData<List<CoinTypeBean>> httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.updateCoinType(httpData.getData());
                        }
                    }
                });
    }
    public void getMyMoney(String code){
        httpService.withdrawList()
                .converter(new JsonConverter<HttpData<TradeAccountBean>>() {})
                .adapt(new FlowableBody<>())
                .map(listHttpData -> listHttpData.getData().getRes().getAsset())
                .flatMap(rechargeTypeBeans -> Flowable.fromIterable(rechargeTypeBeans))
                .filter(rechargeTypeBean -> rechargeTypeBean.getPname().equals(code))
                .subscribe(rechargeTypeBean -> {
                    mView.updateLeftMoney(rechargeTypeBean.getUsable());
                },e->{
                    System.out.println(e);
                });
    }

    public void requsetPayWayList() {
        OkGo.<HttpData<List<PayWayListBean>>>get(HttpConfig.BASE_URL + HttpConfig.PAY_WAY_LIST)
                .params("account", SPUtil.get(SPConfig.ACCOUNT, ""))
                .execute(new JsonCallBack<HttpData<List<PayWayListBean>>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<List<PayWayListBean>>> response) {
                        if (response.body().getStatus() == HttpConfig.OK) {
                            mView.updatePayUi(response.body().getData());
                        }
                    }
                });
    }

    public void requsetServiceCharge(String type) {
        OkGo.<HttpData<ServiceChargeBean>>get(HttpConfig.BASE_URL + HttpConfig.SHELLRECORD)
                .params("type", type)
                .execute(new JsonCallBack<HttpData<ServiceChargeBean>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<ServiceChargeBean>> response) {
                        if (response.body().getStatus() == HttpConfig.OK) {
                            mView.updateServiceCharge(response.body().getData());
                        }
                    }
                });
    }

    public void Shell(String num, String type, String bankid, String banktp, String tpwd,String code) {
        OkGo.<HttpData<Object>>get(HttpConfig.BASE_URL + HttpConfig.SHELL)
                .params("num", num)
                .params("type", type)
                .params("bankid", bankid)
                .params("banktp", banktp)
                .params("tpwd", CommonUtil.getFiveMd5Value(tpwd))
                .params("code", code)
                .execute(new JsonCallBack<HttpData<Object>>(this, false) {
                    @Override
                    public void onSuccess(Response<HttpData<Object>> response) {
                        if (response.body().getStatus() == HttpConfig.OK) {
                            ToastUtil.showShort(response.message());
                            mView.updateBuyUi();
                        }
                    }
                });
    }

    public void sendCode() {
        String account = SPUtil.get(SPConfig.USER_ACCOUNT, "");
        if (account.contains("@")) {
            sendEmail(account);
        } else {
            sendSms(SPUtil.get(SPConfig.CODE, ""), account, "1");
        }
    }

    public void sendEmail(String email) {
        httpService.sendEmail(email, null).execute(callback);
    }

    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
    public void sendSms(String mcode, String mobile, String type) {

        httpService.sendSms(mcode, mobile, type).execute(callback);
    }

    JsonCallBack<HttpData> callback = new JsonCallBack<HttpData>(this) {
        @Override
        public void onSuccess(Response<HttpData> response) {
            HttpData httpData = response.body();
            if (httpData.getStatus() == HttpConfig.OK) {
                mView.sendCheckCodeSuccess();
            } else {
                ToastUtil.showShort(httpData.getMsg());
                mView.sendCheckCodeFail();
            }
        }

        @Override
        public void onError(Response<HttpData> response) {
            mView.sendCheckCodeFail();
            super.onError(response);
        }
    };
}
